import Cookies from 'js-cookie'

const app = {
  state: {
    sidebar: {
      opened: !+Cookies.get('sidebarStatus')
    },
    theme: 'default',
    system_id: 2, //门店管理系统
    keywords: '',  //全局搜索关键词
    shop_id: Cookies.get('shopid') ? parseInt(Cookies.get('shopid')) : 0,     //全局门店ID
    livenewsChannels: Cookies.get('livenewsChannels') || '[]',
    visitedViews: []
  },
  mutations: {
    TOGGLE_SIDEBAR: state => {
      if (state.sidebar.opened) {
        Cookies.set('sidebarStatus', 1);
      } else {
        Cookies.set('sidebarStatus', 0);
      }
      state.sidebar.opened = !state.sidebar.opened;
    },
    ADD_VISITED_VIEWS: (state, view) => {
      if (state.visitedViews.some(v => v.path === view.path)) return
      state.visitedViews.push({name: view.name, path: view.path})
    },
    SET_KEYWORDS: (state, keyword) => {
      state.keywords = keyword;
    },
    SET_SHOP_ID: (state, shop_id) => {
      state.shop_id = parseInt(shop_id);
      Cookies.set('shopid', shop_id)
    }
  },
  actions: {
    ToggleSideBar: ({commit}) => {
      commit('TOGGLE_SIDEBAR')
    },
    addVisitedViews: ({commit}, view) => {
      commit('ADD_VISITED_VIEWS', view)
    },
    set_keywords: ({commit}, keyword) => {
      commit('SET_KEYWORDS', keyword)
    },
    set_shop_id: ({commit}, shop_id) => {
      commit('SET_SHOP_ID', shop_id)
    }
  }
};

export default app;
